package com.bawei.car.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.bawei.car.domain.CarInfo;
import com.bawei.car.domain.CarResult;
import com.bawei.car.domain.response.ShopCarResponse;
import com.bawei.car.domain.response.ShopCarSkuChange;
import com.bawei.car.service.ShopCarService;
import com.bawei.common.core.domain.R;
import com.bawei.mall.product.cache.ProductInfoCache;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bawei.common.log.annotation.Log;
import com.bawei.common.log.enums.BusinessType;
import com.bawei.common.security.annotation.RequiresPermissions;
import com.bawei.car.domain.ShopCar;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.common.core.web.domain.AjaxResult;
import com.bawei.common.core.utils.poi.ExcelUtil;
import com.bawei.common.core.web.page.TableDataInfo;

/**
 * 张氏企业帝国商品总览Controller
 *
 * @author 张氏企业帝国老总
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/car")
@Log4j2
public class ShopCarController extends BaseController
{

    @Autowired
    private ShopCarService shopCarService;

    @Autowired
    private  HttpServletRequest request;

    @Autowired
    ProductInfoCache productInfoCache;

    @GetMapping("/product/{id}")
    public R get(@PathVariable Long id){
        return R.ok(productInfoCache.getData(id));
    }


    /**
     * 查找购物车列表
     * @return
     */
    @PostMapping("/findShopCarList")
    public R findShopCarList(){
        return R.ok(this.shopCarService.findShopCar());
    }

    /**
     * 加入购物车
     * @param shopCar
     * @return
     */
    @Log(title = "张氏企业帝国",businessType = BusinessType.INSERT)
    @PostMapping("/addShopCar")
    public R addShopCar(@RequestBody ShopCar shopCar){
        this.shopCarService.addShopCar(shopCar);
        return R.ok(null,"加入购物车成功");
    }

    /**
     * 修改数量
     * @param shopCar
     * @return
     */
    @Log(title = "张氏企业帝国",businessType = BusinessType.UPDATE)
    @PostMapping("/updateNumber")
    public R updateNumber(@RequestBody ShopCar shopCar){
        this.shopCarService.updateNumber(shopCar);
        return R.ok(null,"");
    }

    /**
     * 修改数量
     * @param shopCarSkuChange
     * @return
     */
    @Log(title = "张氏企业帝国",businessType = BusinessType.UPDATE)
    @PostMapping("/skuChange")
    public R skuChange(@RequestBody ShopCarSkuChange shopCarSkuChange){
        this.shopCarService.skuChange(shopCarSkuChange);
        return R.ok(null,"");
    }

    /**
     * 修改选中
     * @param shopCar
     * @return
     */
    @Log(title = "张氏企业帝国",businessType = BusinessType.UPDATE)
    @PostMapping("/upChecked")
    public R upChecked(@RequestBody ShopCar shopCar){
        this.shopCarService.upChecked(shopCar);
        return R.ok(null,"");
    }


    /**
     * 查询张氏企业帝国商品总览列表
     */
    @RequiresPermissions("car:car:list")
    @GetMapping("/list")
    public TableDataInfo list(ShopCar shopCar)
    {
        startPage();
        List<ShopCar> list = shopCarService.selectShopCarList(shopCar);
        return getDataTable(list);
    }

    /**
     * 导出张氏企业帝国商品总览列表
     */
    @RequiresPermissions("car:car:export")
    @Log(title = "张氏企业帝国商品总览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopCar shopCar)
    {
        List<ShopCar> list = shopCarService.selectShopCarList(shopCar);
        ExcelUtil<ShopCar> util = new ExcelUtil<ShopCar>(ShopCar.class);
        util.exportExcel(response, list, "张氏企业帝国商品总览数据");
    }

    /**
     * 获取张氏企业帝国商品总览详细信息
     */
    @RequiresPermissions("car:car:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(shopCarService.selectShopCarById(id));
    }

    /**
     * 新增张氏企业帝国商品总览
     */
    @RequiresPermissions("car:car:add")
    @Log(title = "张氏企业帝国商品总览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopCar shopCar)
    {
        return toAjax(shopCarService.insertShopCar(shopCar));
    }

    /**
     * 修改张氏企业帝国商品总览
     */
    @RequiresPermissions("car:car:edit")
    @Log(title = "张氏企业帝国商品总览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopCar shopCar)
    {
        return toAjax(shopCarService.updateShopCar(shopCar));
    }

    /**
     * 删除张氏企业帝国商品总览
     */
    @RequiresPermissions("car:car:remove")
    @Log(title = "张氏企业帝国商品总览", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopCarService.deleteShopCarByIds(ids));
    }
}
